/**
 *
 */
package com.qwiki.xml;

/**
 * WikiPageAsyncPrintHandler
 *
 * @author xgu
 * @since Jun 27, 2012
 */
public class WikiPageAsyncPrintHandler extends WikiPageAbstractAsyncHandler {
  private WikiXMLPageFilter filter;

  /**
   * @param name
   * @param id
   */
  public WikiPageAsyncPrintHandler(String name, int id) {
    super(name, id);
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * com.qwiki.xml.WikiPageAbstractAsyncHandler#doProcess(com.qwiki.xml.WikiXMLPage
   * )
   */
  @Override
  public void doProcess(WikiXMLPage xmlpage) {
    if (filter == null || filter.isGood(xmlpage)) {
      try {
        WikiPage page = xmlpage.parse();
        System.out.println(page.getTitle() + "\n" + page.getText());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.qwiki.xml.WikiPageHandler#getFilter()
   */
  @Override
  public synchronized WikiXMLPageFilter getFilter() {
    // TODO Auto-generated method stub
    return filter;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * com.qwiki.xml.WikiPageHandler#setFilter(com.qwiki.xml.WikiXMLPageFilter)
   */
  @Override
  public synchronized void setFilter(WikiXMLPageFilter filter) {
    this.filter = filter;
  }

  private static synchronized void print(String string) {
    System.out.print(string);
  }

  private static synchronized void println(String[] strings) {
    for (String string : strings) {
      System.out.println(string);
    }
  }
  private static synchronized void println(String string) {
    System.out.print(string);
  }
}
